"""
行政区划 | 接口
"""

from fastapi import (
    APIRouter,
    Depends,
)

import utils.enum_utils as enum
from biz import region_biz
from components.audit_log import audit
from components.depends import login_required
from components.global_param import get_user_id
from components.logger import log
from components.response import OKWithDataResponse
from models.request import (
    RegionDetailRequest,
    RegionListRequest,
    RegionPathField,
)
from models.response import (
    RegionDetailResponse,
    RegionListResponse,
)
from models.vo import (
    RegionDetailVO,
    RegionListPage,
)

region_router: APIRouter = APIRouter(prefix="/region")
""" 行政区划 路由 """


@region_router.get(
    path="/",
    summary="行政区划 | 列表",
    response_model=RegionListResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.REGION,
    operation=enum.ModelOperation.LIST,
    po_id="response.data.data_list.id",
)
async def region_list(request: RegionListRequest = Depends(RegionListRequest.from_query)) -> OKWithDataResponse:
    vo: RegionListPage = region_biz.RegionGetter.list(request)
    log.info(f"[行政区划][列表] user_id: {get_user_id()}")

    return OKWithDataResponse(vo)


@region_router.get(
    path="/{code}/",
    summary="行政区划 | 详情",
    response_model=RegionDetailResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.REGION,
    operation=enum.ModelOperation.DETAIL,
    po_id="response.data.id",
)
async def region_detail(code: str = RegionPathField.code) -> OKWithDataResponse:
    request = RegionDetailRequest(code=code)
    vo: RegionDetailVO = region_biz.RegionGetter.detail(request)
    log.info(f"[行政区划][详情] user_id: {get_user_id()} code: {code}")

    return OKWithDataResponse(vo)
